/*
 * test.c
 *
 *  Created on: Oct 3, 2013
 *      Author: cinus
 */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "jjann.h"
//#include "activation.h"
#include "topology.h"
#include "training.h"
#include "constants.h"
#include "settings.h"
#include "errorfunctions.h"
#include "activation.h"

int main(int argc, char** argv) {

	uint32 status;
	uint32 j;
	float32 error;
	netdata_tp net;
	settings_tp settings;
	trainingData_t trainingData;
	trainingData_t validationData;
	uint32 maxHiddenNeurons;
	float32 epsilon;

	maxHiddenNeurons = 25;
	epsilon = .5f;

	settings = (settings_tp) malloc(sizeof(settings_t));
	loadSettings(settings, "configs/iris.cfg");

	net = (netdata_tp) calloc(1,sizeof(netdata_t));

	srand(settings->seed);
	if (settings == NULL) {
		fprintf(stderr, "Error loading settings, aborting software\n");
		exit(EXIT_FAILURE);
	} else {
		fprintf(stderr, "Settings loaded\n");
	}

	status = loadSamples(&trainingData, "datasets/iris.training");
	if (status != EXIT_SUCCESS) {
		fprintf(stderr, "Error, aborting software\n");
		exit(status);
	} else {
		fprintf(stderr, "Training data loaded\n");
	}

	status = loadSamples(&validationData, "datasets/iris.validation");
	if (status != EXIT_SUCCESS) {
		fprintf(stderr, "Error, aborting software\n");
		exit(status);
	} else {
		fprintf(stderr, "Validation data loaded\n");
	}
	status = incrementalPruning(net, epsilon, settings->maxEpochs, maxHiddenNeurons, &trainingData, &validationData, activationTanh, diffActivationTanh, activationSoftmax, diffActivationSoftmax, crossEntropy, settings);
	fprintf(stderr, "Status := %d\n", status);
	if (status == EXIT_SUCCESS) {
		fprintf(stderr,"Evaluating the generated network\n");
		error = validate(net, validationData.samples, validationData.sampleSize);
		net->error = error;
		fprintf(stderr, "Best Network [%d %d %d] has an error of %f\n", net->neurons[0], net->neurons[1], net->neurons[2], net->error);
		destroyNetwork(net);
	}else{
		fprintf(stderr, "Not able to find a network with a maximum of %d hidden neurons and an error < %f\n",maxHiddenNeurons, epsilon);
	}
	freeSamples(&trainingData);
	freeSamples(&validationData);
	free(settings->algorithm);
	free(settings->topologyFileName);
	free(settings);
	return 0;
}
